import { createRouter, createWebHistory } from 'vue-router'           // 引入 vue-router 所需模块
import IndexView from '../views/Index.vue'                            // 引入主框架页组件

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),               // 创建 HTML5 路由历史对象
  routes: [
    {
      path: '/',                                                     // 根路径重定向
      redirect: '/login'
    },
    {
      path: '/',                                                     // 一级路径（主框架页）
      name: 'index',
      component: IndexView,
      children: [                                                    // 嵌套路由
        {
          path: 'home',
      name: 'home',
          component: () => import('../views/home/HomeView.vue'),
    },
    {
          path: 'cart',
          name: 'cart',
          component: () => import('../views/cart/CartView.vue'),
        },
        {
          path: 'chat',
          name: 'chat',
          component: () => import('../views/chat/ChatView.vue'),
          children: [
            {
              path: '',
              name: 'chatList',
              component: () => import('../views/chat/ChatListView.vue')
            },
            {
              path: 'detail/:roomId',
              name: 'chatDetail',
              component: () => import('../views/chat/ChatDetailView.vue'),
              props: true
            }
          ]
        },
        {
          path: 'main',
          name: 'main',
          component: () => import('../views/main/MainView.vue')      // 我的页
        }
      ]
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('@/views/home/SearchView.vue')         // 搜索页
    },
    {
      path: '/xiangqing',
      name: 'xiangqing',
      component: () => import('@/views/home/Xiangqingview.vue')      // 商品详情页
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/LoginView.vue')        // 登录页
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/register/Register.vue')      // 注册页
    },
    {
      path: '/order',
      name: 'order',
         component: () => import('@/views/main/OrderView.vue')          // 订单页
    },
    {
      path: '/forget',
      name: 'forget',
      component: () => import('@/views/forget/Forget.vue')           // 忘记密码页
    },
    {
      path: '/yzm',
      name: 'yzm',
      component: () => import('@/views/yam/Yzm.vue')                 // 验证码页
    },
    {
      path: '/setmima',
      name: 'setmima',
      component: () => import('@/views/setmima/Setmima.vue')         // 重设密码页
    },
    {
      path: '/ziliao',
      name: 'ziliao',
      component: () => import('@/views/ziliao/Ziliao.vue')           // 资料页
    },
    {
      path: '/zhifu',
      name: 'zhifu',
      component: () => import('@/views/zhifu/Zhifu.vue')             // 支付页
    },
    {
      path: '/activity',
      name: 'activity',
      component: () => import('@/views/home/ActivityView.vue')       // 活动页
    },
    {
      path: '/shopping',
      name: 'shopping',
      component: () => import('@/views/home/shopping.vue')           // 购物记录页
    },
    {
      path:'/OrderConfirmed',
      name:'OrderConfirmed',
      component: () => import('@/views/main/OrderView.vue'),
    },
    {
      path:'/OrderConfirmed1',
      name:'OrderConfirmed1',
      component: () => import('@/views/cart/OrderConfirmedView.vue'),
    },
    {
      path:'/EditAddress',
      name:'EditAddress',
      component: () => import('../views/cart/EditAddress.vue'),
    },
    {
      path:'/PayView',
      name:'PayView',
      component:()=>import('@/views/cart/PayView.vue'),
    },
    {
      path:'/PayTJ',
      name:'PayTJ',
      component:()=>import('@/views/cart/PayTJ.vue'),
    },

  ],
})

// 路由导航守卫，判断用户是否已登录
router.beforeEach((to, from, next) => {
  const isLoggedIn = () => {
    return localStorage.getItem('username') ? true : false           // 模拟登录状态检查
  }

  if (to.path === '/' && to.name !== 'login') {
    console.log('访问根路径，重定向到登录页')
    return next({ name: 'login' })
  }

  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)

  if (requiresAuth && !isLoggedIn()) {
    console.log('路由需要认证但用户未登录，重定向到登录页')
    return next({ name: 'login' })
  } else if (!requiresAuth && isLoggedIn() && to.name === 'login') {
    console.log('用户已登录，尝试访问登录页，重定向到首页')
    return next({ name: 'home' })
  } else {
    next()                                                           // 正常放行
  }
})

export default router                                                // 导出路由实例
